﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ERPStore.Extensions;

using Microsoft.Practices.Unity;

namespace ERPStore.Host.Start
{
	public class HostConfig
	{
		internal static void ConfigureSettings(Microsoft.Practices.Unity.IUnityContainer container, string rootPath)
		{
			// Resolution et chargement des paramètres de configuration du site
			var settingsService = container.Resolve<ERPStore.Services.ISettingsService>();
			var settings = settingsService.GetWebSiteSettings();
			if (settings == null) // First run
			{
				throw new Exception("Settings does not exists");
			}

			var tempPath = ERPStore.Configuration.ConfigurationSettings.AppSettings["tempPath"] ?? System.IO.Path.GetTempPath();
			settings.TempPath = tempPath;
			settings.ImagePath = ERPStore.Configuration.ConfigurationSettings.AppSettings["imagePath"] ?? tempPath;
			var address = ERPStore.Configuration.ConfigurationSettings.AppSettings["webapiAddress"] ?? "http://locahost:9080";
			settings.CurrentUrl = address;
			settings.PhysicalPath = rootPath;

			GlobalConfiguration.Configuration.Settings = settings;

			// Configuration du cryptage
			var injectionMembers = new InjectionMember[] 
			{ 
				new InjectionConstructor(GlobalConfiguration.Configuration.Settings.CryptoKey, GlobalConfiguration.Configuration.Settings.CryptoIV) ,
			};
			container.RegisterType<ERPStore.Services.CryptoService>(new ContainerControlledLifetimeManager(), injectionMembers);

			Console.WriteLine("settings configured");
		}

	}
}
